<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="班次" prop="timeShiftName">
        <el-select
          v-model="queryParams.timeShiftName"
          placeholder="班次"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in timeShiftList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="考勤状态" prop="attendanceFlag">
        <el-select
          v-model="queryParams.attendanceFlag"
          placeholder="考勤状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in attendanceFlagParma"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="渠道商" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入渠道商"
          clearable
          size="small"
          style="width: 200px"

        />
      </el-form-item>
      <el-form-item label="姓名" prop="workerName">
        <el-input
          v-model="queryParams.workerName"
          placeholder="请输入姓名"
          clearable
          size="small"
          style="width: 200px"
        />
      </el-form-item>



      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

      <el-row :gutter="10" class="mb8">

        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['job:attendanceGroup:detail:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="workerName">
              <el-input v-model="form.workerName" placeholder="请输入姓名" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证" prop="idCard">
              <el-input v-model="form.idCard"  placeholder="请输入身份证"  maxlength="32" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phoneCode">
              <el-input v-model="form.phoneCode" placeholder="请输入联系电话" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组别" prop="groupName">
              <el-input v-model="form.groupName" placeholder="请输入组别"  />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>

          <el-col :span="12">
            <el-form-item label="班次" prop="timeShiftName">
              <el-select v-model="form.timeShiftName"  placeholder="请选择" @change='handleChangetimeShif'>
                <el-option
                  v-for="item in timeShiftOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.name"

                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="性别">
              <el-select v-model="form.sex" placeholder="请选择">
                <el-option
                  v-for="dict in sexOptions"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="招聘渠道" prop="companyName">
              <el-select v-model="form.companyName"  placeholder="请选择" @change='handleChangeCompany'  >
                <el-option
                  v-for="item in companyOptions"
                  :key="item.id"
                  :label="item.company"
                  :value="item.company"

                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改考勤状态对话框 -->
    <el-dialog :title="title" :visible.sync="openA" width="600px" append-to-body>
      <el-form ref="formA" :model="formA" :rules="ruleA" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="workerName">
              <el-input v-model="formA.workerName" placeholder="请输入姓名" maxlength="30" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证" prop="idCard">
              <el-input v-model="formA.idCard"  placeholder="请输入身份证"  maxlength="32" readonly/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="考勤状态"  prop="attendanceFlag">
              <el-select v-model="formA.attendanceFlag" placeholder="请选择">
                <el-option
                  v-for="dict in attendanceFlagOptions"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注">
              <el-input v-model="formA.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormA">确 定</el-button>
        <el-button @click="cancelA">取 消</el-button>
      </div>
    </el-dialog>


    <el-table v-loading="loading" :data="attendanceGroupList"  @selection-change="handleSelectionChange"  :row-class-name="tableRowClassName">
      <!--       <el-table-column type="selection" width="40" align="center"/>
            <el-table-column label="序号"  type="index" width="60" align="center"/>-->
      <el-table-column label="姓名" prop="workerName" min-width="80"/>
      <el-table-column label="身份证" prop="idCard" :show-overflow-tooltip="true" min-width="180"/>
      <el-table-column label="联系电话" prop="phoneCode" :show-overflow-tooltip="true" min-width="120"/>
      <el-table-column label="组别"  prop="groupName" align="center" min-width="100"/>
      <el-table-column label="班次"  prop="timeShiftName" align="" min-width="80"/>
      <el-table-column label="性别" align="center" prop="sex" min-width="80">
        <template slot-scope="scope">
          <div v-if="scope.row.sex == 1">男</div>
          <div v-else-if="scope.row.sex == 2">女</div>
        </template>
      </el-table-column>
      <el-table-column label="招聘渠道" prop="companyName" min-width="180"/>

      <!--       <el-table-column label="来源渠道" prop="sourceFlag" width="100">-->
      <!--         <template slot-scope="scope">-->
      <!--           <div v-if="scope.row.sourceFlag == 1">excel导入</div>-->
      <!--           <div v-else-if="scope.row.sourceFlag == 2">小程序</div>-->
      <!--         </template>-->
      <!--       </el-table-column>-->
      <el-table-column label="考勤状态" align="center" prop="attendanceFlag" min-width="80">
        <template slot-scope="scope">
          <div v-if="scope.row.attendanceFlag == 0">未知</div>
          <div v-if="scope.row.attendanceFlag == 1">异常</div>
          <div v-else-if="scope.row.attendanceFlag == 2">正常</div>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" min-width="100"/>
      <el-table-column label="数据异常" align="center" prop="statusFlag" min-width="80">
        <template slot-scope="scope">
          <div v-if="scope.row.statusFlag == 0">数据缺失</div>
          <div v-if="scope.row.statusFlag == 1">正常</div>

        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.checkFlag !==3 "
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['job:attendanceGroup:detail:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.checkFlag !==3 "
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleStatus(scope.row)"
            v-hasPermi="['job:attendanceGroup:detail:change']"
          >修改状态</el-button>

          <el-button
            v-if="scope.row.checkFlag !==3 "
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['job:attendanceGroup:detail:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />



  </div>
</template>
<style>
.el-table .warning-row {
  background: #FF8888;
}

</style>
<script>
import {
  listAttendanceSum,getAttendanceGroup,
  delAttendanceGroup,updateAttendanceGroup,
  addAttendanceGroup,exportAttendance
}
  from "@/api/job/attendance/attendanceApi";


export default {
  name: "detail",
  created() {
    this.getList();

  },
  data() {
    return {
      sexOptions:[{value: 1, label: '男'}, {value: 2, label: '女'}],
      timeShiftList:[{value: 1, label: '白班'}, {value: 2, label: '夜班'}],
      attendanceFlagOptions:[{value: 0, label: '请选择'},{value: 2, label: '正常'},{value: 1, label: '异常'}],
      attendanceFlagParma:[{value: -1, label: '请选择'},{value: 0, label: '未知'},{value: 2, label: '正常'},{value: 1, label: '异常'}],
      signUpLoading:true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      attendanceGroupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      openA: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      signUp:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        //渠道名称
        groupName: undefined,
        timeShiftName: undefined,
        attendanceFlag:-1,
        //联系人
        workerName: undefined,
        workTime: this.$route.query.workTime,
        orderCode: this.$route.query.orderCode,
      },

      companyOptions: [],
      groupOptions: [],
      timeShiftOptions:[],
      // 表单参数
      form: {},
      formA: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
    // 表单校验
    rules: {
      workerName: [
        { required: true, message: "姓名不能为空", trigger: "blur" },
        { min: 2, max: 20, message: '姓名长度必须介于 2 和 20 之间', trigger: 'blur' }
      ],

        groupName: [
        { required: true, message: "班组不能为空", trigger: "blur" }
      ],
      sex: [
        { required: true, message: "性别不能为空", trigger: "blur" }
      ],
      companyName: [
        { required: true, message: "招聘渠道不能为空", trigger: "blur" }
      ],
        timeShiftName: [
        { required: true, message: "班次不能为空", trigger: "blur" }
      ],
        phoneCode: [
        {
          pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
          message: "请输入正确的手机号码",
          trigger: "blur"
        }
      ]
    },
      ruleA: {
        attendanceFlag: [
          {  pattern:/^[12]$/, message: "考勤状态只能修改为异常或者正常", trigger: "blur" }
        ],

      },


    };
  },


  methods: {
    tableRowClassName({row, rowIndex}) {
      if (row.statusFlag==0) {
        return 'warning-row';
      }
      return '';

  },
    /** 获取员工信息列表 */
    getList() {
      this.loading = true;
      listAttendanceSum(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.attendanceGroupList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelA() {
      this.openA = false;
      this.reset();
    },
    // 表单重置
    reset() {
        this.form = {
          id:undefined,
          groupName: undefined,
          idCard: undefined,
          phoneCode: undefined,
          workerName: undefined,
          timeShiftName: undefined,
          sex: undefined,
          company: undefined,
          staffSign: undefined,
          bz: undefined
        };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getAttendanceGroup().then(response => {

        this.companyOptions = response.companys;
        this.open = true;
        this.title = "添加";

      });
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAttendanceGroup(id).then(response => {
        this.form = response.data;
        this.companyOptions = response.companys;
        this.form.companyName = response.data.companyName;
        this.groupOptions = response.sysPost;
        this.form.postName = response.data.postName;
        this.timeShiftOptions = response.timeShift;
        this.form.timeShiftName = response.data.timeShiftName;

        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.attendanceFlag=2;
          if (this.form.id != undefined) {
            updateAttendanceGroup(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttendanceGroup(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 提交按钮 */
    submitFormA: function() {
      this.$refs["formA"].validate(valid => {
        if (valid) {
          this.formA.statusFlag=1;
          if (this.formA.id != undefined) {
            updateAttendanceGroup(this.formA).then(response => {
              this.msgSuccess("修改成功");
              this.openA = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids
      this.$confirm('是否确认删除编号为"' + id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delAttendanceGroup(id);
      }).then(() => {
        this.handleQuery();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },

    handleStatus(row) {
      this.reset();
      const id = row.id || this.ids
      getAttendanceGroup(id).then(response => {
        this.formA = response.data;

        this.openA = true;
        this.title = "修改考勤状态";
      })
    },
    handleExport(){
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出员工考勤数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportAttendance(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    },

    handleChangetimeShif(value){
      let choosenItem = this.timeShiftOptions.filter(
        item => item.name === value)[0];
      let id=choosenItem.id;
      this.form.timeShift =id;
    },
    handleChangeCompany(value){
      let choosenItem = this.companyOptions.filter(
        item => item.company === value)[0];
      let id=choosenItem.id;
      this.form.companyId =id;
      this.$forceUpdate();
    },
  }
};
</script>
